package edu.guet.server.controller;

import edu.guet.server.model.dto.*;
import edu.guet.server.service.ResourceService;
import edu.guet.server.utils.AliOSSUtils;
import edu.guet.server.utils.ResultAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/resources")
public class ResourceController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    ResourceService resourceService;

    /**
     * 教师端在视频库里添加视频
     * @param resourceDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/addvideo")
    public ResultAPI addVideo(ResourceDTO resourceDTO){
        log.info("addvideo:{}",resourceDTO);
        //更新时间
//        resourceDTO.setUpdate_time(DatetimeSwitch.parseLocalDateTimeToString(LocalDateTime.now()));
        String url;
        //处理file
        try{
            url=aliOSSUtils.upload(resourceDTO.getFile());
        }catch (Exception e){
            log.info("文件存OSS失败！");
            return ResultAPI.error("文件上传失误，请求失败！");
        }
        //无转码，我不会！
//        url="https://mynoteapp-web.oss-cn-beijing.aliyuncs.com/a1bbf73d-3111-4e0e-b519-8500b99dc80b.mp4";
        resourceDTO.setResource_url(url);

        //开始保存视频url
        Integer reid = resourceService.addResourceUrl(resourceDTO.getResource_name(),url,"video");
        resourceDTO.setResource_id(reid);
        //保存老师资源关系
        resourceService.addTechRe(resourceDTO);
        return ResultAPI.success("添加视频成功！");
    }
    /**
     * 教师端在资料库里添加PPT
     * @param resourceDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/addPPt")
    public ResultAPI addppt(ResourceDTO resourceDTO){
        String url;
////        处理file
        try{
            url=aliOSSUtils.upload(resourceDTO.getFile());
        }catch (Exception e){
            log.info("文件存OSS失败！");
            return ResultAPI.error("文件上传失误，请求失败！");
        }
//        url="https://mynoteapp-web.oss-cn-beijing.aliyuncs.com/f2e80ddf-828e-486f-bf95-548a90fe4661.pptx";
        resourceDTO.setResource_url(url);
        //开始保存视频url
        Integer reid = resourceService.addResourceUrl(resourceDTO.getResource_name(),url,"ppt");
        resourceDTO.setResource_id(reid);
        //保存老师资源关系
        resourceService.addTechRe(resourceDTO);
        return ResultAPI.success(resourceDTO,"添加视频成功！");
    }
    /**
     * 教师端展示老师视频列表
     */
    @GetMapping("/showvideos")
    public ResultAPI showVideo(@RequestParam Integer course_id){
        log.info("展示很多资料:{}",course_id);
        List<ResourceDTO> showlist=resourceService.showResources(course_id,"video");
        return ResultAPI.success(showlist);
    }
    /**
     * 教师端展示老师资料列表
     */
    @GetMapping("/showppts")
    public ResultAPI showPpt(@RequestParam Integer course_id){
        log.info("展示很多视频:{}",course_id);
        List<ResourceDTO> showlist=resourceService.showResources(course_id,"ppt");
        return ResultAPI.success(showlist);
    }
    @PostMapping("/addStuResource")
    public ResultAPI addStuResource(@RequestBody StuReDTO stuReDTO){
        log.info("addStuResource:{}",stuReDTO);
        StuReDTO stuRe =resourceService.selectStuRe(stuReDTO);
        if(stuRe!=null){
            return ResultAPI.success("记录过了！");
        }else {
            resourceService.addStuResource(stuReDTO);
            return ResultAPI.success("记录成功！");
        }
    }
    @PostMapping("/watchvideo")
    public ResultAPI watchVideo(@RequestBody WatchUnitReDTO watchUnitReDTO){
        WatchUnitReDTO temp=resourceService.selectWatchResource(watchUnitReDTO);
        if(temp==null){
            resourceService.addWatchUniRe(watchUnitReDTO);
            return ResultAPI.success("添加时长！");
        }else {
            resourceService.updateWatchRe(watchUnitReDTO);
            return ResultAPI.success("时长已更新！");
        }
    }

    /**
     * 教师端删除资料
     *
     * @param resource_id 要删除的资料ID
     * @return 操作结果
     */
    @DeleteMapping("/deleteResource/{resource_id}")
    public ResultAPI deleteResource(@PathVariable Integer resource_id) {
        log.info("deleteResource:{}",resource_id);
            resourceService.deleteCourseResource(resource_id);
            return ResultAPI.success("资料删除成功");

    }

    /**
     * 教师端修改资料名字
     *
     * @return 操作结果
     */
    @PutMapping("/changeResourceName")
    public ResultAPI changeResourceName(ResourceDTO resourceDTO) {
        log.info("changeResourceName:{}",resourceDTO);
        resourceService.changeResourceName(resourceDTO.getResource_id(), resourceDTO.getResource_name());
        return ResultAPI.success("资料名字修改成功");

    }

    @GetMapping("/checkResource")
    public ResultAPI checkResource(@RequestParam Integer unit_id,@RequestParam Integer user_id) {
        List<CheckResource> list=resourceService.getCheckResource(unit_id,user_id);
        return ResultAPI.success(list);
    }

    @GetMapping("/showUnitsandRes")
    public ResultAPI showUnitsandRes(@RequestParam Integer semester_id) {
        List<UnitReList> showlist=resourceService.getUnitsRes(semester_id);
        log.info("ShowUnitsandRes:{}",showlist);
        return ResultAPI.success(showlist);
    }

}
